package engine.tip
{
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.GameDefine;
	import engine.newUi.MyPropertyLogic;
	import engine.tip.tipItem.BaseTipItem;
	import engine.tip.tipItem.CompareEqItem;
	import engine.tip.tipItem.ComposeTipItem;
	import engine.tip.tipItem.EquipItem;
	import engine.tip.tipItem.ITipItem;
	import engine.tip.tipItem.KungfuItem;
	import engine.tip.tipItem.MedicineItem;
	import engine.tip.tipItem.NormalItem;
	import engine.tip.tipItem.NormalPartnerItem;
	import engine.tip.tipItem.PartnerItem;
	import engine.tip.tipItem.SkillItemTips;
	import engine.tip.tipItem.SpecialKunfuItem;
	import engine.tip.tipItem.StrideFighterItem;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import newx.BaseObject;
	import newx.StageInfo;

	/**
	 *主要逻辑 
	 * @author yanghr
	 * 
	 */
	public class TipsMainLogic
	{
		public static const CONFIG_ID:String="ConfigID";
		private  static var _me:TipsMainLogic;
		/**
		 * X_AWAY		鼠标与tips x间距
		 * Y_AWAY		鼠标与tips y间距
		 * PANEL_AWAY	对比tips 间距
		 * 
		 */		
		public var X_AWAY:int = 20, Y_AWAY:int = 20,PANEL_AWAY:int=2;
		/**
		 *tips责任表 
		 */		
		private var CHAIN_OF_RESIPOSIBLITY:Object=new Object;
	
		private var _timer:Timer=new Timer(2000);
		private var _mousePoint:Point;
		private var _nowPoint:Point;
		private var _checkLen:Number	=	250;
		
		private var _text:TextField=new TextField;
		
		private var _stage:Stage;
		/**
		 *当前显示的tips 
		 */		
		private var _mainPanel:TipsPanelUI;
		private var _normalPanel:TipsPanelUI;
		private var _comparePanel:TipsPanelUI;
		/**
		 *跨服战专用 
		 */		
		private var _stridePanel:StrideTipsPanelUI;
		
		private var _eqItem:EquipItem;
		private var _eqCompareItem:CompareEqItem;
		private var _partnerItem:PartnerItem;
		private var _normalPartnerItem:NormalPartnerItem;
		private var _normalItem:NormalItem;
		private var _skillItem:SkillItemTips;
		private var _composeItem:ComposeTipItem;
		private var _specialKunfuItem:SpecialKunfuItem;
		private var _medicineItem:MedicineItem;
		private var _kungfuItem:KungfuItem;
		
		private var _strideItem:StrideFighterItem;

		public function set isAutoSet(value:Boolean):void
		{
			_mainPanel.isAutoSet	=	value;
		}

		/**
		 *丹药 
		 * @param view
		 * @param data
		 * @return 
		 * 
		 */		
		public function getMedicine(view:ObjectData=null,data:Object=null):MedicineItem
		{
			return getItem(view,data,_medicineItem) as MedicineItem
		}
		public function reSetPosition():void
		{
			X_AWAY 		= 20;
			Y_AWAY 		= 20;
			PANEL_AWAY	= 2;
		}
		/**
		 *一般tips显示规则 
		 * @param view
		 * @return 
		 * 
		 */		
		public function getNormalItem(view:ObjectData=null,data:Object=null):NormalItem
		{
			return getItem(view,data,_normalItem) as NormalItem
		}
		public function getComposeItem(view:ObjectData=null,data:Object=null):ComposeTipItem
		{
			return getItem(view,data,_composeItem) as ComposeTipItem;
		}
		public function getSpecialKunfuItem(view:ObjectData=null,data:Object=null):SpecialKunfuItem
		{
			return getItem(view,data,_specialKunfuItem) as SpecialKunfuItem;
		}
		private function getItem(view:ObjectData,data:Object,item:BaseTipItem):BaseTipItem
		{
			if(view)
			{
				item.dataObj=view;
			}
			if(data)
			{
				item.data=data;
			}
			return item;
		}
		/**
		 *装备tips 被比较的 
		 * @param view
		 * @return 
		 * 
		 */		
		public function getEqCompareItem(view:ObjectData,data:Object=null):CompareEqItem
		{
			return getItem(view,data,_eqCompareItem) as CompareEqItem;
		}
		/**
		 * 装备tips
		 * @param view
		 * @return 
		 * 
		 */		
		public function getEqItem(view:ObjectData,data:Object=null):EquipItem
		{
			return  getItem(view,data,_eqItem) as EquipItem;
		}
		/**
		 *邀请伙伴tipsItem 
		 * @param view
		 * @return 
		 * 
		 */		
		public function getPartnerItem(view:ObjectData,data:Object=null):PartnerItem
		{
			return  getItem(view,data,_partnerItem) as PartnerItem;
		}
		/**
		 *伙伴tipsItem 
		 * @param view
		 * @return 
		 * 
		 */		
		public function getNormalPartnerItem(view:ObjectData,data:Object=null):PartnerItem
		{
			return  getItem(view,data,_normalPartnerItem) as NormalPartnerItem;
		}
		/**
		 *功法 
		 * @param view
		 * @param data
		 * @return 
		 * 
		 */		
		public function getKungfuItem(view:ObjectData,data:Object=null):KungfuItem
		{
			return  getItem(view,data,_kungfuItem) as KungfuItem;
		}

		/**
		 *技能TIPs 
		 * @param obj
		 * @return 
		 * 
		 */
		public function getSkillItem(obj:Object):SkillItemTips
		{
			 _skillItem.data = obj;
			 return _skillItem;
		}

		public static function get instance():TipsMainLogic
		{
			if(!_me)
			{
				_me	= new TipsMainLogic;
			}
			return _me;
		}
		public function TipsMainLogic()
		{
			
			_normalPanel	=	new TipsPanelUI();
			_comparePanel	=	new TipsPanelUI;
			_stridePanel	=	new StrideTipsPanelUI;
			_mainPanel	=	getPanelByItem(null);
			_eqItem=new EquipItem(null);
			_partnerItem=new PartnerItem(null);
			_eqCompareItem=new CompareEqItem();
			_normalItem=new NormalItem();
			_timer.addEventListener(TimerEvent.TIMER,checkHide);
			_stage=StageInfo.stage;
			_skillItem = new SkillItemTips(null);
			_composeItem=new ComposeTipItem();
			_specialKunfuItem = new SpecialKunfuItem();
			_medicineItem=new MedicineItem;
			_kungfuItem=new KungfuItem;
			_normalPartnerItem=new NormalPartnerItem;
			_mousePoint=new Point;
			_nowPoint=new Point;
			initChain();
			reSetPosition();
		}
		private function initChain():void
		{
			CHAIN_OF_RESIPOSIBLITY[GameDefine.ItemType_SpecialKungfu] = getSpecialKunfuItem;
			CHAIN_OF_RESIPOSIBLITY[GameDefine.ItemType_COMPOSE]=getComposeItem;
			CHAIN_OF_RESIPOSIBLITY[GameDefine.ItemType_health]=getMedicine;
			CHAIN_OF_RESIPOSIBLITY[GameDefine.ItemType_KUNGFU]=getKungfuItem;
		}
		
		//		switch(objData.GetPropertiesEx("ItemType"))
		//		{
		//			case GameDefine.ItemType_COMPOSE:
		//			{
		//				showByItem(getComposeItem(objData))
		//				break;
		//			}
		//				
		//			default:
		//			{
		//				showByItem(getNormalItem(objData));
		//				break;
		//			}
		//		}
		/**
		 *根据configid 显示对应物品tips 
		 * @param id
		 * @param needCompare
		 * 
		 */		
		public function showByConfigId(id:uint,needCompare:Boolean=true):void
		{
			var obj:Object=getStaticObjWithID(id+"");
			showByAll(obj,needCompare);
		}
		/**
		 *显示item方法 
		 * @param item
		 * @param compareItem
		 * 
		 */		
		public function showByItem(item:ITipItem,compareItem:ITipItem=null):void
		{
			_mainPanel	=	getPanelByItem(item);
			showSingleItem(_mainPanel, item);
			if (compareItem)
			{
				showSingleItem(_comparePanel, compareItem);
			}
			show(Boolean(compareItem));
		}
		/**
		 *设置主显示容器 
		 * @param item
		 * 
		 */		
		private function getPanelByItem(item:ITipItem):TipsPanelUI
		{
			if(item is StrideFighterItem)
			{
				return _stridePanel
			}
			else
			{
				return	_normalPanel;
			}
		}
		/**
		 * 主要入口
		 * @param data(支持ObjectData  或者 Object 或者 String 或者int型 的configid)
		 * @param needCompare
		 * 
		 */		
		public function showMain(data:*,needCompare:Boolean=true):void
		{
//			if(data is ObjectData)
//			{
//				showByObjData(data as ObjectData,needCompare)
//			}
//			else
//			{
//				showByObj(data,needCompare);
//			}
			if(!data)return;
			if(data is int||data is uint||!isNaN((data)))
			{
				showByConfigId(int(data),needCompare);
			}
			else if(data is String)
			{
				showString(data as String);
			}
			else
			{
				showByAll(data,needCompare);
			}
		}
		private function showByAll(obj:*,needCompare:Boolean=false):void
		{
			var plist:Array=getPlist(obj)
			var targetItem:ITipItem;
			//装备\
			if(checkProperty(obj,"EquipType"))
			{
				if(needCompare)
				{
					var eqo:ObjectData=getSameEqInActor(getAProperty(obj,"EquipType"))
					if(eqo)
					{
						eqo.HandlePropertiesEx(EquipItem.CURRENT_LEVEL,ModelLocator.getInstance().scene.Role.getPlayerLevel());
						showByItem(getEqItem.apply(this,plist),getEqCompareItem.apply(this,getPlist(eqo)));
						eqo.DeletePropertiesEx(EquipItem.CURRENT_LEVEL);
						return ;
					}
				}		
					targetItem=getEqItem.apply(this,plist)
			}
			else
			{
				var func:Function	=	CHAIN_OF_RESIPOSIBLITY[getAProperty(obj,"ItemType")];
				if(func!=null)
				{
					targetItem	=	func.apply(this,plist)
				}
				else
				{
					targetItem	=	getNormalItem.apply(this,plist)
				}
			}
			showByItem(targetItem);
			function getPlist(obj:Object):Array
			{
				var arr:Array
				if(obj is ObjectData)
				{
					arr=[obj]
				}
				else
				{
					arr=[null,obj]
				}
				return arr;
			}
			function getAProperty(obj:*,propertName:String):*
			{
				if(obj is BaseObject)
				{
					return obj.GetPropertiesEx(propertName)
				}
				else
				{
					return obj[propertName];
				}
			}
			function checkProperty(obj:*,propertyName:String):*
			{
				if(obj is BaseObject)
				{
					return obj.CheckProperties(propertyName)
				}
				else
				{
					return obj[propertyName];
				}
			}
		}
		private function getSameEqInActor(eqType:String):ViewObject
		{
			var viewid:int
			var view:View;
			if(MyPropertyLogic.getInstance().ui.created)
			{
				viewid=MyPropertyLogic.getInstance().getEquipView();
			}
			else
			{
				viewid=View.VIEWPORT_EQUIP;
			
			}
			view=ModelLocator.getInstance().scene.GetView(viewid);
			for  (var property:String in view.Properties) 
			{
				var i:int=int(property);
				if(i&&(view.Properties[i] as ViewObject).GetPropertiesEx("EquipType")==eqType)
				{
					return view.Properties[i] as ViewObject
				}
			}
			return null;
			
		}
		private function showSingleItem(_panel:TipsPanelUI,item:ITipItem):void
		{
			_panel.clear();
			item.panel=_panel;
			item.buildTip();
		}
		private function combiObj(objdata:ObjectData):Object
		{
			var obj:Object = getConfigIdObj(objdata.GetPropertiesEx(CONFIG_ID));
			for (var property:String in objdata.Properties)
			{
				if (objdata.CheckProperties(property)&&objdata.GetPropertiesEx(property))
				{
					obj[property] = objdata.GetPropertiesEx(property);
				}
			}
			return obj
		}
		private function getConfigIdObj(id:String):Object
		{
			return _normalItem.getConfigIdObj(id)
//			if(id)
//			{
//				var obj:Object= StaticIni.getIniObj(id);
//				var newObj:Object=new Object();
//				for  (var i:String in obj) 
//				{
//					newObj[i]=obj[i];
//				}
//				return newObj;
//			}
//			return null;
		}
		/**
		 * 
		 * @param target
		 * @param func  function():String{return ""};
 		 * 
		 */		
		public function objRollshowTips(target:DisplayObject,func:Function):void
		{
			target.addEventListener(MouseEvent.ROLL_OVER,function(e:Event):void{showString(func())});
			target.addEventListener(MouseEvent.ROLL_OUT,hide);
		}
		/**
		 *显示字符串 
		 * @param str
		 * 
		 */		
		public function showString(str:String):void
		{
			if(!str)return ;
			_mainPanel	=	getPanelByItem(null);
			_mainPanel.clear();
			_mainPanel.addText(str);
			show();
		}
		/**
		 *强制长tips 
		 * @param str
		 * 
		 */		
		public function showWideString(str:String,width:int=-1):void
		{
			_mainPanel	=	getPanelByItem(null);
			_mainPanel.clear();
			_mainPanel.longString=true;
			_mainPanel.addText(str);
			if(width!=-1) _mainPanel.longTextWidth=width;
			show();
			_mainPanel.longTextWidth = 210;
		}
		/**
		 *得到带有颜色的html文字 
		 * @param str
		 * @param color
		 * @return 
		 * 
		 */		
		public function getColorHtml(str:String,color:String="f00000"):String
		{
			return "<font color='#" + color + "'>" + str + "</font>";
		}
		/**
		 *得到带有下划线的文字 
		 * @param str
		 * @param herf
		 * @return 
		 * 
		 */		
		public function getUnderLineHtml(str:String,herf:String):String
		{
			_text.htmlText=herf;
			herf=_text.text;
			return "<u><a href='event:"+herf+"'>"+str+"</a></u>";
		}
		/**
		 *得到物品（一定会有ConfigID） 
		 * @param configID
		 * @return 
		 * 
		 */		
		public function getStaticObjWithID(configID:String):Object
		{
			var obb:Object=getConfigIdObj(configID);
			if(obb	&&	!obb[CONFIG_ID])
				obb[CONFIG_ID]=configID;
			return obb;
		}
		/**
		 *显示方法 
		 * @param isCompare
		 * 
		 */		
		private function show(isCompare:Boolean=false):void
		{
			_stage.addChild(_mainPanel);
			_mainPanel.visible	=	true;
			if(isCompare)
			{
				_comparePanel.visible	=	true;
			}
			var mainWid:int	=	isCompare?(_mainPanel.contentWidth+_comparePanel.contentWidth):_mainPanel.contentWidth;
			var mainHeig:int=	_mainPanel.height
			var xx:Number,xxx:Number;
			if(_stage.mouseX+mainWid+X_AWAY>_stage.stageWidth)	
			{
				xx	=	(_stage.mouseX	-	_mainPanel.contentWidth	-X_AWAY);	
				if(isCompare)
				{
					xxx	=	(_stage.mouseX	-	mainWid	-X_AWAY-PANEL_AWAY)
				}
			}
			else
			{
				xx	=	((_stage.mouseX	+	X_AWAY));
				if(isCompare)
				{
					xxx	=	_stage.mouseX+_mainPanel.contentWidth	+	X_AWAY	+	PANEL_AWAY;	
				}
			}
			var yy:Number,yyy:Number	;
			if(_stage.mouseY+mainHeig>_stage.stageHeight)	
			{
				yyy	=	yy=	(_stage.mouseY	-	mainHeig	+	Y_AWAY);
				
			}
			else
			{
				yyy	=	yy=	((_stage.mouseY	+	Y_AWAY))
			}
			if(!_mainPanel.parent)
			{
				_stage.addChild(_mainPanel);
			}
			_mainPanel.x	=	xx;
			_mainPanel.y	=	yy;
			_mainPanel.followMouse=true;
			if(isCompare)
			{
				if(!_comparePanel.parent)
				{
					_stage.addChild(_comparePanel);
				}
				_comparePanel.x	=	xxx;
				_comparePanel.y	=	yyy;
				_comparePanel.followMouse=true;
			}
			else
			{
				_comparePanel.visible	=	false;
			}
			_timer.start();
			_mousePoint.x=_stage.mouseX;
			_mousePoint.y=_stage.mouseY;
		}
		private function checkHide(e:TimerEvent):void
		{
//			trace("TipsMainLogic.checkHide(e)");
//			trace(mouseX_show,mouxeY_show,_stage.mouseX,_stage.mouseY)
//			trace(Math.sqrt(Math.abs(mouseX_show*mouseX_show+mouxeY_show*mouxeY_show-_stage.mouseX*_stage.mouseX-_stage.mouseY*_stage.mouseY)))
			_nowPoint.x=_stage.mouseX;
			_nowPoint.y=_stage.mouseY
			if(Point.distance(_mousePoint,_nowPoint)>_checkLen)
			{
				hide();
				_timer.stop();
			}
		}
		/**
		 * 
		 * @param args  可以为空
		 * 
		 */		
		public  function hide(...args):void
		{
			if(_mainPanel)
			{
				_mainPanel.visible	=	false;
				_comparePanel.visible	=	false;
				_mainPanel.followMouse=_comparePanel.followMouse=false;
			}
		}
		/**
		 *当前tips 是不是在显示中 
		 * @return 
		 * 
		 */		
		public function get isMainShow():Boolean
		{
			return _mainPanel.visible;
		}
		/**
		 *对比tips 是不是在显示 
		 * @return 
		 * 
		 */		
		public function get isCompareShow():Boolean
		{
			return _comparePanel.visible;
		}
	}
}